﻿using UnityEngine;
using UnityEngine.UI;

/// <summary>
/// 反转Toggle事件
/// </summary>
[RequireComponent(typeof(Toggle))]
public class ReverseToggleEvent : MonoBehaviour
{
    public Toggle.ToggleEvent onValueChangedReverse = new Toggle.ToggleEvent();

    private Toggle m_Toggle;
    private Toggle toggle => m_Toggle ??= GetComponent<Toggle>();

    private void Awake()
    {
        onValueChangedReverse.Invoke(!toggle.isOn);
        toggle.onValueChanged.AddListener(OnValueChangedReverse);
    }

    private void OnValueChangedReverse(bool isOn)
    {
        onValueChangedReverse?.Invoke(!isOn);
    }

    private void OnDestroy()
    {
        toggle.onValueChanged.RemoveListener(OnValueChangedReverse);
    }
}